package com.imooc.controller;

import com.imooc.pojo.Stu;
import com.imooc.service.StuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@ApiIgnore
@RestController
public class HelloController {
    public static final Logger logger = LoggerFactory.getLogger(HelloController.class);

    @Autowired
    private StuService stuService;

    @GetMapping("/hello")
    public String hello(){
        logger.debug("debug:msg");
        logger.info("info:msg");
        logger.warn("warn:msg");
        logger.error("error:msg");
        int i = 1/0;
        return "hello world";
    }

    @GetMapping("/getStuInfo/{id}")
    private Object getStuInfo(@PathVariable("id") int id){
        Stu stuInfo = stuService.getStuInfo(id);
        return stuInfo;
    }

    @PostMapping("/saveStu")
    private String saveStu(Stu stu){
        stuService.saveStu(stu);
        return "ok";
    }

    @GetMapping("/setSession")
    public Object setSession(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("userInfo","cyl");
        session.setMaxInactiveInterval(3600);
        session.getAttribute("userInfo");
        session.removeAttribute("userInfo");
        return "ok";
    }
}
